package com.segersten.othello.service.rest;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.DefaultHandler;
import org.eclipse.jetty.server.handler.HandlerList;
import org.eclipse.jetty.server.nio.SelectChannelConnector;
import org.eclipse.jetty.server.session.SessionHandler;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;

import com.google.common.collect.ImmutableMap;
import com.sun.jersey.api.core.PackagesResourceConfig;
import com.sun.jersey.spi.container.servlet.ServletContainer;

public class JettyUtil {

	public static void start(String filename) throws Exception {
		// TODO use xml file
		/*
		Resource serverXml = Resource.newSystemResource(filename);
		XmlConfiguration configuration = new XmlConfiguration(serverXml.getInputStream());
		Server server = (Server) configuration.configure();
		server.start();
		server.join();
		*/

		Server server = new Server();
		SelectChannelConnector connector = new SelectChannelConnector();
		connector.setPort(8999);
		server.addConnector(connector);

		HandlerList handlers = new HandlerList();
		handlers.setHandlers(new Handler[] { createJerseyServlet(), createHelloWorldServlet(), new DefaultHandler() });
		server.setHandler(handlers);

		server.start();
		server.join();	
	}

	/*private static Handler createJerseyServlet2() {
		ServletContextHandler handler = new ServletContextHandler();
		handler.setContextPath("/game");
		SessionHandler sessionHandler = new SessionHandler();
		handler.setSessionHandler(sessionHandler);
		ServletHolder holder = handler.addServlet("com.sun.jersey.spi.container.servlet.ServletContainer", "/*");
		Map<String,String> initParams = ImmutableMap.of(
				"com.sun.jersey.config.property.packages", "com.segersten.othello.service.rest",
				"com.sun.jersey.api.json.POJOMappingFeature", "true");
		holder.setInitParameters(initParams);
		return handler;
	}*/
	
	private static Handler createJerseyServlet() {
		ServletContextHandler handler = new ServletContextHandler();
		handler.setContextPath("/game");
		SessionHandler sessionHandler = new SessionHandler();
		handler.setSessionHandler(sessionHandler);
		Map<String,Object> initMap = new HashMap<String, Object>();
	    initMap.put("com.sun.jersey.api.json.POJOMappingFeature", "true");
	    initMap.put("com.sun.jersey.config.property.packages", "com.segersten.othello.service.rest");
	    ServletHolder servletHolder = new ServletHolder(new ServletContainer(new PackagesResourceConfig(initMap)));
	    handler.addServlet(servletHolder, "/*");
		return handler;
	}
	
	private static Handler createHelloWorldServlet() {
		ServletContextHandler handler = new ServletContextHandler();
		handler.setContextPath("/hello");
		SessionHandler sessionHandler = new SessionHandler();
		handler.setSessionHandler(sessionHandler);
		handler.addServlet(new ServletHolder(new HelloServlet()),"/*");
		handler.addServlet(new ServletHolder(new HelloServlet("Buongiorno Mondo")),"/it/*");
		handler.addServlet(new ServletHolder(new HelloServlet("Bonjour le Monde")),"/fr/*");
		return handler;
	}
}

class HelloServlet extends HttpServlet
{
    private String greeting="Hello World";
    public HelloServlet(){}
    public HelloServlet(String greeting)
    {
        this.greeting=greeting;
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        response.setContentType("text/html");
        response.setStatus(HttpServletResponse.SC_OK);
        response.getWriter().println("<h1>"+greeting+"</h1>");
        response.getWriter().println("session=" + request.getSession(true).getId());
    }
}
